package cn.jtfadmin.core.modules.sys.domain.entity;



import cn.jtfadmin.base.data.jpa.domain.entity.BaseEntity;
import cn.jtfadmin.base.filesystem.annos.File;
import cn.jtfadmin.base.lang.common.domain.entity.BuildInAbleEntity;
import cn.jtfadmin.base.lang.enums.BooleanEnum;
import cn.jtfadmin.base.lang.enums.FileType;
import cn.jtfadmin.core.constant.SysUserConstants;
import cn.jtfadmin.core.enums.SexEnum;
import cn.jtfadmin.core.enums.UserStatusEnum;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.time.LocalDate;
import java.util.List;

/**
 * 系统用户
 */

@Getter
@Setter
@Entity
@Table(name = "sys_user")
public class SysUserEntity extends BaseEntity implements BuildInAbleEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 是否内置用户，内置用户不可删除
     */
    @Column(name = "build_in_flag", updatable = false)
    protected BooleanEnum buildInFlag;

    /**
     * 登录账号
     */
    @Column(name = "username", unique = true, length = 200)
    @NotBlank
    @Size(max = 200, min = 1)
    protected String username;

    /**
     * 真实姓名
     */
    @Column(name = "realname", length = 100, nullable = false)
    @NotBlank
    @Size(max = 100, min = 1)
    protected String realname;

    /**
     * 密码
     */
    @Column(name = "password", length = 100, nullable = false)
    @NotBlank
    @Size(max = 100, min = 1)
    protected String password;


    /**
     * 头像
     */
    @Column(name = "headimg")
    @File(dirName = SysUserConstants.SYS_USER_AVATAR_FILE_TYPE, fileType = FileType.IMAGE, describe = "用户头像")
    protected String headimg;

    /**
     * 生日
     */
    @Column(name = "birthday")
    protected LocalDate birthday;

    /**
     * 性别
     */
    @Column(name = "sex")
    @Enumerated(value = EnumType.STRING)
    protected SexEnum sex;

    /**
     * 电子邮件
     */
    @Column(name = "email", length = 50, unique = true)
    @Email
    @Size(max = 50)
    protected String email;

    /**
     * 电话
     */
    @Column(name = "phone", length = 20, unique = true)
    @Size(max = 20)
    protected String phone;

    /**
     * 状态 NORMAL:正常, FROZEN:冻结
     */
    @Column(name = "status")
    @NotNull
    protected UserStatusEnum status;


    /**
     * 用户角色
     */
    @ManyToMany()
    @JoinTable(name = "sys_user_role", inverseJoinColumns = @JoinColumn(name = "role_id"), joinColumns = @JoinColumn(name = "user_id"))
    protected List<SysRoleEntity> roleList;


    /**
     * 部门
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_user_depart", joinColumns = @JoinColumn(name = "sys_user_id"),
            inverseJoinColumns = @JoinColumn(name = "sys_depart_id"))
    protected SysDepartEntity sysDepart;

    /**
     * 负责部门
     */
    @ManyToMany()
    @JoinTable(name = "sys_depart_leader_user", joinColumns = @JoinColumn(name = "sys_user_id"),
            inverseJoinColumns = @JoinColumn(name = "sys_depart_id"))
    protected List<SysDepartEntity> leaderDeparts;

}
